import router from "./router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { getStore } from "@/utils/store";

NProgress.configure({ showSpinner: false });

const whiteList = ["Login", "404", "Friend", "Match", "Divination", "Year"];

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const hasAccountId = getStore("account_id");
  if (hasAccountId) {
    if (to.path === "/login") {
      next({ path: "/" });
      NProgress.done();
    } else {
      next();
      NProgress.done();
    }
  } else {
    console.log(to);
    if (whiteList.indexOf(to.name) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
